<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item label="资源名称" prop="name">
        <el-input
          v-model="queryParams.name"
          class="!w-240px"
          clearable
          placeholder="请输入资源名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版权编号" prop="copyrightNumber">
        <el-input
          v-model="queryParams.copyrightNumber"
          class="!w-240px"
          clearable
          placeholder="请输入版权编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版权状态" prop="copyrightStatus">
        <el-select
          v-model="queryParams.copyrightStatus"
          class="!w-240px"
          placeholder="请选择版权状态"
          style="width: 240px"
          @keyup.enter="handleQuery"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="openForm('新增')">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          查找
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['system:role:create']"
          plain
          type="primary"
          @click="openDownloadExpire()"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          下架过期资源
        </el-button>
        <el-button v-hasPermi="['system:role:create']" plain type="primary" @click="handleActive()">
          <Icon class="mr-5px" icon="ep:plus" />
          批量上传
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="ID" prop="id" :width="50" />
      <el-table-column align="center" label="资源名称" prop="name" />
      <el-table-column align="center" label="资源类型" prop="type" />
      <el-table-column align="center" label="版权编号" prop="copyrightNumber" />
      <el-table-column align="center" label="版权有效期" prop="copyrightValidity" />
      <el-table-column align="center" label="版权状态" prop="copyrightStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.copyrightStatus == '正常'" type="success">{{
            scope.row.copyrightStatus
          }}</el-tag>
          <el-tag v-else="scope.row.copyrightStatus == '已过期'" type="danger">{{
            scope.row.copyrightStatus
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openForm('查看详情', scope.row.id)"
          >
            查看
          </el-button>
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openForm('编辑', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            v-hasPermi="['system:role:delete']"
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
          <el-button
            v-if="scope.row.copyrightStatus === '已过期'"
            link
            type="primary"
            @click="handleActive()"
          >
            下架资源
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!-- <CopyrightForm ref="formRef" @success="getList" /> -->
  <!-- 表单弹窗：下架过期资源 -->
  <DownloadExpire ref="refExpire" @success="getList" />
</template>
<script lang="ts" setup>
import * as microCopyrightApi from '@/api/copyright/microCopyright'
import DownloadExpire from './details/DownloadExpire.vue'
import { DICT_TYPE } from '@/utils/dict'
defineOptions({ name: 'Copyright' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: '',
  copyrightNumber: '',
  copyrightStatus: '',
  createTime: []
})
/** 版权状态 */
const stateOptions = [
  {
    value: '0',
    label: '正常'
  },
  {
    value: '1',
    label: '已过期'
  }
]
const options = [
  {
    value: '',
    label: '全部'
  },
  {
    value: '正常',
    label: '正常'
  },
  {
    value: '已过期',
    label: '已过期'
  }
]
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await microCopyrightApi.getMicroCopyrightPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 下架过期资源操作 */
const refExpire = ref()
const openDownloadExpire = async () => {
  refExpire.value.open()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改/查看操作 */
const openForm = (type: string, id?: number) => {
  if (type === '新增') {
    push({
      name: 'addMicroCopyRight',
      query: {
        type: type,
        id: id
      }
    })
  } else {
    push({
      name: 'updateMicroCopyRight',
      query: {
        type: type,
        id: id
      }
    })
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await microCopyrightApi.deleteMicroCopyright(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 未开发按钮操作 */
const handleActive = async () => {
  return message.success(t('功能未开发！'))
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
